using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Spread.Common.Features.Sample;
using FarPoint.Win;
using FarPoint.Win.Spread;
using FarPoint.Win.Spread.CellType;
using FarPoint.Win.Spread.Model;
using Spread.Common.DataStore;

namespace Spread.Worksheet.Features
{
    public partial class OperationModesDemo : BaseSampleControl
    {
        //private MultiOptionCellType _multiOptional = new MultiOptionCellType();
        public OperationModesDemo()
        {
            InitializeComponent();
            InitializeSample();
        }
        private void InitializeSample()
        {
            MultiOptionCellType _multiOptional = new MultiOptionCellType();
            Name = "Selection and Operation Modes";
            spreadMain.Sheets[0].Rows.Default.Height = 25;
            spreadMain.DataSource = new DataService().AccessDataSource.GetNWINDData("Customers");
            sheetViewMain.AlternatingRows[1].BackColor = Color.WhiteSmoke;
            sheetViewControl.Cells[0, 0].Locked = true;
            sheetViewControl.Cells[0, 0].Text = "OperationMode:";
            _multiOptional.Items = Enum.GetNames(typeof(OperationMode));
            _multiOptional.Orientation = FarPoint.Win.RadioOrientation.Vertical;
            sheetViewControl.Cells[1, 0].CellType = _multiOptional;
            sheetViewControl.Cells[1, 0].RowSpan = 5;
            sheetViewControl.Cells[1, 0].Value = sheetViewMain.OperationMode;
            sheetViewControl.Cells[0, 1].Locked = true;
            sheetViewControl.Cells[0, 1].Text = "SelectionBlockOptions:";
            CheckBoxCellType _checkBoxCellType = new CheckBoxCellType();
            _checkBoxCellType.Caption = Enum.GetName(typeof(SelectionBlockOptions), SelectionBlockOptions.None);
            sheetViewControl.Cells[1, 1].CellType = _checkBoxCellType;
            sheetViewControl.Cells[1, 1].Tag = SelectionBlockOptions.None;
            sheetViewControl.Cells[1, 1].Value = (spreadMain.SelectionBlockOptions == SelectionBlockOptions.None);
            _checkBoxCellType = new CheckBoxCellType();
            _checkBoxCellType.Caption = Enum.GetName(typeof(SelectionBlockOptions), SelectionBlockOptions.Cells);
            sheetViewControl.Cells[2, 1].CellType = _checkBoxCellType;
            sheetViewControl.Cells[2, 1].Tag = SelectionBlockOptions.Cells;
            sheetViewControl.Cells[2, 1].Value = ((spreadMain.SelectionBlockOptions & SelectionBlockOptions.Cells) == SelectionBlockOptions.Cells);
            _checkBoxCellType = new CheckBoxCellType();
            _checkBoxCellType.Caption = Enum.GetName(typeof(SelectionBlockOptions), SelectionBlockOptions.Rows);
            sheetViewControl.Cells[3, 1].CellType = _checkBoxCellType;
            sheetViewControl.Cells[3, 1].Tag = SelectionBlockOptions.Rows;
            sheetViewControl.Cells[3, 1].Value = ((spreadMain.SelectionBlockOptions & SelectionBlockOptions.Rows) == SelectionBlockOptions.Rows);
            _checkBoxCellType = new CheckBoxCellType();
            _checkBoxCellType.Caption = Enum.GetName(typeof(SelectionBlockOptions), SelectionBlockOptions.Columns);
            sheetViewControl.Cells[4, 1].CellType = _checkBoxCellType;
            sheetViewControl.Cells[4, 1].Tag = SelectionBlockOptions.Columns;
            sheetViewControl.Cells[4, 1].Value = ((spreadMain.SelectionBlockOptions & SelectionBlockOptions.Columns) == SelectionBlockOptions.Columns);
            _checkBoxCellType = new CheckBoxCellType();
            _checkBoxCellType.Caption = Enum.GetName(typeof(SelectionBlockOptions), SelectionBlockOptions.Sheet);
            sheetViewControl.Cells[5, 1].CellType = _checkBoxCellType;
            sheetViewControl.Cells[5, 1].Tag = SelectionBlockOptions.Sheet;
            sheetViewControl.Cells[5, 1].Value = ((spreadMain.SelectionBlockOptions & SelectionBlockOptions.Sheet) == SelectionBlockOptions.Sheet);
            sheetViewControl.SetOutlineBorder(new CellRange(1, 1, 5, 1), new LineBorder(Color.Black));
            _multiOptional = new MultiOptionCellType();
            sheetViewControl.Cells[6, 0].Locked = true;
            sheetViewControl.Cells[6, 0].Text = "SelectionPolicy:";
            _multiOptional.Items = Enum.GetNames(typeof(SelectionPolicy));
            _multiOptional.Orientation = FarPoint.Win.RadioOrientation.Vertical;
            sheetViewControl.Cells[7, 0].CellType = _multiOptional;
            sheetViewControl.Cells[7, 0].Value = sheetViewMain.SelectionPolicy;
            _multiOptional = new MultiOptionCellType();
            sheetViewControl.Cells[6, 1].Locked = true;
            sheetViewControl.Cells[6, 1].Text = "SelectionUnit:";
            _multiOptional.Items = Enum.GetNames(typeof(SelectionUnit));
            _multiOptional.Orientation = FarPoint.Win.RadioOrientation.Vertical;
            sheetViewControl.Cells[7, 1].CellType = _multiOptional;
            sheetViewControl.Cells[7, 1].Value = sheetViewMain.SelectionUnit;
            sheetViewControl.Columns[0].Width = 150;
            sheetViewControl.Columns[1].Width = 180;
            sheetViewControl.Rows[7].Height = 60;
            sheetViewControl.HorizontalGridLine = new GridLine(GridLineType.None);
            sheetViewControl.VerticalGridLine = new GridLine(GridLineType.None);
        }

        private void SpreadControl_ButtonClicked(object sender, EditorNotifyEventArgs e)
        {
            if (e.Row == 1 && e.Column == 0)
            { // OperationMode
                object _selectedValue = sheetViewControl.Cells[1, 0].Value;
                if (_selectedValue is int)
                {
                    sheetViewMain.ClearSelection();
                    sheetViewMain.OperationMode = (OperationMode)_selectedValue;
                    if ((int)_selectedValue > 1 )
                    {   // disable other options 
                        sheetViewControl.Cells[6, 0, 7, 1].Locked = true;
                        sheetViewControl.Cells[0, 1, 5, 1].Locked = true;
                    }
                    else
                    {   // enable other options
                        sheetViewControl.Cells[6, 0, 7, 1].Locked = false;
                        sheetViewControl.Cells[0, 1, 5, 1].Locked = false;
                    }
                }
            }
            else if ( 0 < e.Row && e.Row < 6 && e.Column == 1)
            { // SelectionBlockOptions
                object value = sheetViewControl.Cells[e.Row, 1].Value;
                bool isChecked = value is bool && (bool)value == true;
                SelectionBlockOptions _opt = (SelectionBlockOptions)sheetViewControl.Cells[e.Row, 1].Tag;
                if (isChecked)
                {
                    sheetViewMain.ClearSelection();
                    spreadMain.SelectionBlockOptions = _opt == 0 ? _opt : spreadMain.SelectionBlockOptions | _opt;
                    if (_opt == 0)
                        sheetViewControl.Cells[2, 1, 5, 1].Value = false;
                    else
                        sheetViewControl.Cells[1, 1].Value = false;
                }
                else if (_opt > 0)
                {
                    sheetViewMain.ClearSelection();
                    spreadMain.SelectionBlockOptions = spreadMain.SelectionBlockOptions & ~_opt;
                }
            }
            else if (e.Row == 7 && e.Column == 0)
            { // SelectionPolicy (Normal or Read Only mode only)
                object _selectedValue = sheetViewControl.Cells[7, 0].Value;
                if (_selectedValue is int)
                {
                    sheetViewMain.ClearSelection();
                    sheetViewMain.SelectionPolicy = (SelectionPolicy)_selectedValue;
                }
            }
            else if( e.Row== 7 && e.Column == 1)
            { // SelectionUnit (Normal or Read Only mode only)
                object _selectedValue = sheetViewControl.Cells[7, 1].Value;
                if (_selectedValue is int)
                {
                    sheetViewMain.ClearSelection();
                    sheetViewMain.SelectionUnit = (SelectionUnit)_selectedValue;
                }
            }
        }
    }
}


//
//OperationModesDemo.Designer.cs
//


namespace Spread.Worksheet.Features
{
    partial class OperationModesDemo
    {
        /// <summary> 
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code

        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.spreadMain = new FarPoint.Win.Spread.FpSpread();
            this.sheetViewMain = new FarPoint.Win.Spread.SheetView();
            this.spreadControl = new FarPoint.Win.Spread.FpSpread();
            this.sheetViewControl = new FarPoint.Win.Spread.SheetView();
            this.pnlMain.SuspendLayout();
            this.pnlControls.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.spreadMain)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.sheetViewMain)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.spreadControl)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.sheetViewControl)).BeginInit();
            this.SuspendLayout();
            // 
            // pnlMain
            // 
            this.pnlMain.Controls.Add(this.spreadMain);
            // 
            // pnlControls
            // 
            this.pnlControls.Controls.Add(this.spreadControl);
            // 
            // spreadMain
            // 
            this.spreadMain.Dock = System.Windows.Forms.DockStyle.Fill;
            this.spreadMain.Location = new System.Drawing.Point(0, 0);
            this.spreadMain.Name = "spreadMain";
            this.spreadMain.Sheets.AddRange(new FarPoint.Win.Spread.SheetView[] {
            this.sheetViewMain});
            this.spreadMain.Size = new System.Drawing.Size(477, 487);
            this.spreadMain.TabIndex = 0;
            // 
            // sheetViewMain
            // 
            this.sheetViewMain.Reset();
            this.sheetViewMain.SheetName = "Main Sheet";
            // Formulas and custom names must be loaded with R1C1 reference style
            this.sheetViewMain.ReferenceStyle = FarPoint.Win.Spread.Model.ReferenceStyle.R1C1;
            this.sheetViewMain.ColumnCount = 15;
            this.sheetViewMain.RowCount = 100;
            this.sheetViewMain.ActiveColumnIndex = 1;
            this.sheetViewMain.ActiveRowIndex = 2;
            this.sheetViewMain.Cells.Get(2, 1).Font = new System.Drawing.Font("Times New Roman", 8.25F);
            this.sheetViewMain.Columns.Get(1).Width = 175F;
            this.sheetViewMain.ReferenceStyle = FarPoint.Win.Spread.Model.ReferenceStyle.A1;
            // 
            // spreadControl
            // 
            this.spreadControl.DefaultSkin = FarPoint.Win.Spread.DefaultSpreadSkins.Default;
            this.spreadControl.Dock = System.Windows.Forms.DockStyle.Fill;
            this.spreadControl.Location = new System.Drawing.Point(0, 0);
            this.spreadControl.Name = "spreadControl";
            this.spreadControl.Sheets.AddRange(new FarPoint.Win.Spread.SheetView[] {
            this.sheetViewControl});
            this.spreadControl.Size = new System.Drawing.Size(157, 487);
            this.spreadControl.TabIndex = 0;
            this.spreadControl.ButtonClicked += new FarPoint.Win.Spread.EditorNotifyEventHandler(this.SpreadControl_ButtonClicked);
            // 
            // sheetViewControl
            // 
            this.sheetViewControl.Reset();
            this.sheetViewControl.SheetName = "Sheet1";
            // Formulas and custom names must be loaded with R1C1 reference style
            this.sheetViewControl.ReferenceStyle = FarPoint.Win.Spread.Model.ReferenceStyle.R1C1;
            this.sheetViewControl.ColumnCount = 2;
            this.sheetViewControl.RowCount = 8;
            this.sheetViewControl.DefaultStyle.Font = new System.Drawing.Font("Verdana", 8.25F);
            this.sheetViewControl.ReferenceStyle = FarPoint.Win.Spread.Model.ReferenceStyle.A1;
            // 
            // OperationModesDemo
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Name = "OperationModesDemo";
            this.pnlMain.ResumeLayout(false);
            this.pnlControls.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.spreadMain)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.sheetViewMain)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.spreadControl)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.sheetViewControl)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion

        private FarPoint.Win.Spread.FpSpread spreadMain;
        private FarPoint.Win.Spread.SheetView sheetViewMain;
        private FarPoint.Win.Spread.FpSpread spreadControl;
        private FarPoint.Win.Spread.SheetView sheetViewControl;
    }
}
